package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.system.controller.CxBase;
import com.ruoyi.system.domain.AdvertisementInfo;
import com.ruoyi.system.mapper.AdvertisementInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.PageRecommendMapper;
import com.ruoyi.system.domain.PageRecommend;
import com.ruoyi.system.service.IPageRecommendService;

/**
 * 页面推荐Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-07
 */
@Service
public class PageRecommendServiceImpl extends CxBase implements IPageRecommendService
{
    @Autowired
    private PageRecommendMapper pageRecommendMapper;
    @Autowired
    private AdvertisementInfoMapper advertisementInfoMapper;

    /**
     * 查询页面推荐
     * 
     * @param id 页面推荐ID
     * @return 页面推荐
     */
    @Override
    public PageRecommend selectPageRecommendById(Long id)
    {
        PageRecommend pageRecommend =  pageRecommendMapper.selectPageRecommendById(id);
        pageRecommend.setRecommendDate(StampToString(Long.valueOf(pageRecommend.getRecommendDate())));
        pageRecommend.setSetDate(StampToString(Long.valueOf(pageRecommend.getSetDate())));
        return pageRecommend;
    }

    /**
     * 查询页面推荐列表
     * 
     * @param pageRecommend 页面推荐
     * @return 页面推荐
     */
    @Override
    public List<PageRecommend> selectPageRecommendList(PageRecommend pageRecommend)
    {
        startPage();
        List<PageRecommend> pageRecommends = pageRecommendMapper.selectPageRecommendList(pageRecommend);
        if(pageRecommends.size()>0) {
            for (PageRecommend pageRecommend1 : pageRecommends) {
                pageRecommend1.setRecommendDate(StampToString(Long.valueOf(pageRecommend1.getRecommendDate())));
                pageRecommend1.setSetDate(StampToString(Long.valueOf(pageRecommend1.getSetDate())));
            }
        }
        return pageRecommends;
    }

    /**
     * 新增页面推荐
     * 
     * @param pageRecommend 页面推荐
     * @return 结果
     */
    @Override
    public int insertPageRecommend(PageRecommend pageRecommend)
    {
        if(!checkNull(pageRecommend.getRecommendMain())&&!checkNull(pageRecommend.getRecommendType())&&!checkNull(pageRecommend.getSetId())){
            switch (pageRecommend.getRecommendType()){
                case "0":
                    if(!checkNull(pageRecommend.getAdvertisementId())){
                        AdvertisementInfo advertisementInfo = new AdvertisementInfo();
                        advertisementInfo = advertisementInfoMapper.selectAdvertisementInfoById(pageRecommend.getAdvertisementId());
                        pageRecommend.setRecommendContent(advertisementInfo.getAdvertisementContent());
                        pageRecommend.setRecommendPic(advertisementInfo.getAdvertisementPic());
                        pageRecommend.setRecommendTitle(advertisementInfo.getAdvertisementTitle());
                    }
                    else return 0;
                    break;
                case "1":
                    pageRecommend.setRecommendContent("/pages/examination/index");
                    break;
                case "2":
                    pageRecommend.setRecommendContent("/pages/propaganda/index");
                    break;
                case "3":
                    pageRecommend.setRecommendContent("/pages/callpolice/index");
                    break;
                default:
                    return 0;
            }
            pageRecommend.setRecommendDate(getStamp().toString());
            pageRecommend.setSetDate(getStamp().toString());
            return pageRecommendMapper.insertPageRecommend(pageRecommend);
        }
        return 0;
    }

    /**
     * 修改页面推荐
     * 
     * @param pageRecommend 页面推荐
     * @return 结果
     */
    @Override
    public int updatePageRecommend(PageRecommend pageRecommend)
    {
        if(!checkNull(pageRecommend.getRecommendMain())&&!checkNull(pageRecommend.getRecommendType())&&!checkNull(pageRecommend.getSetId())){
            switch (pageRecommend.getRecommendType()){
                case "0":
                    if(!checkNull(pageRecommend.getAdvertisementId())){
                        AdvertisementInfo advertisementInfo = new AdvertisementInfo();
                        advertisementInfo = advertisementInfoMapper.selectAdvertisementInfoById(pageRecommend.getAdvertisementId());
                        pageRecommend.setRecommendContent(advertisementInfo.getAdvertisementContent());
                        pageRecommend.setRecommendPic(advertisementInfo.getAdvertisementPic());
                        pageRecommend.setRecommendTitle(advertisementInfo.getAdvertisementTitle());
                    }
                    else return 0;
                    break;
                case "1":
                    pageRecommend.setRecommendContent("/pages/examination/index");
                    break;
                case "2":
                    pageRecommend.setRecommendContent("/pages/propaganda/index");
                    break;
                case "3":
                    pageRecommend.setRecommendContent("/pages/callpolice/index");
                    break;
                default:
                    return 0;
            }
            pageRecommend.setRecommendDate(getStamp().toString());
            pageRecommend.setSetDate(getStamp().toString());
            return pageRecommendMapper.updatePageRecommend(pageRecommend);
        }
        return 0;
    }

    /**
     * 批量删除页面推荐
     * 
     * @param ids 需要删除的页面推荐ID
     * @return 结果
     */
    @Override
    public int deletePageRecommendByIds(Long[] ids)
    {
        if(ids.length>0){
           return pageRecommendMapper.deletePageRecommendByIds(ids);
        }
        return 0;
    }

    /**
     * 删除页面推荐信息
     * 
     * @param id 页面推荐ID
     * @return 结果
     */
    @Override
    public int deletePageRecommendById(Long id)
    {
        return pageRecommendMapper.deletePageRecommendById(id);
    }
}
